import Taro, { Component } from '@tarojs/taro'
import { AtForm, AtInput, AtButton } from 'taro-ui'
import { View, Text } from '@tarojs/components'
import { DNavBar, DModal, DAgree, DAtInpute, DPaymodel } from '@/components'
import './transportorder.scss'
import http from '@/api'
const getUrlScheme = http.getUrlScheme
export default class transportorder extends Component {

  constructor(props) {
    super(props)
    this.state = {
      active: 0,
      contact: '',
      isOpened: false,
      item: {},
      contactinfo: '',
      _objinfo: {},
      _status: false,
      check: false,
      creatorId: "",
      creator: ""
    }
  }
  componentWillMount() {
    if (window.localStorage.getItem('ISPAY')) {
      // this.state.isOpened： = true
      this.setState({
        isOpened: true
      }, () => window.localStorage.removeItem('ISPAY'))
    }
    let { creatorId = '', creator = '', qpId = '' } = this.$router.params
    this.setState({
      creatorId,
      creator,
      qpId
    })
    this.getOrderTrans()
  }

  setObjInfo(key, value) {
    if (key) {
      this.setState({
        _objinfo: Object.assign(this.state._objinfo, { [key]: value })
      })
    }
  }

  getOrderTrans() {
    let { qpId = "" } = this.$router.params
    let obj = {
      qpId: `${qpId}`
    }
    http.getOrderTrans(obj).then(res => {
      this.setState({
        item: res
      })
    })
  }
  payFuc(type) {
    this.setState({
      isOpened: false
    })
    if (type) {
      Taro.redirectTo({
        url: `/pages/consignment/consignment?active=1`
      })
    }
  }
  onCancelPay(payType) {
    this.state.paramsPay.payType = payType
    http.addOrderTrans(this.state.paramsPay).then(async (response) => {
      if (+payType === 1) {
        window.localStorage.setItem('ISPAY', 1)
      }
      if (!response) {
        return Taro.showToast({
          title: '订单状态异常',
          icon: 'none'
        })
      }
      if (payType == 7) {
        let url = encodeURIComponent(response)
        return Taro.navigateTo({
          url: `/pages/wxQrCode?payUrl=${url}`
        })
      }
      if (payType == 8) {
        // 小程序支付
        const urlScheme = await getUrlScheme(response, 0)
        window.location.href = urlScheme
        return
      }
      window.location.href = response
      //"&redirect_url=" + "/#/pages/consignment/consignment?active=1"

      // setTimeout(() => {
      //   Taro.redirectTo({
      //     url: `/pages/consignment/consignment?active=1`
      //   })
      // }, 1000)


    })
  }
  addOrderTrans(payType) {
    let { item, _objinfo, _status, check, creator, creatorId, qpId } = this.state
    let _objkey = ['contact', 'contactinfo', 'aimcontact', 'aimcontactinfo']
    let objtext = {
      contact: '请输入始发地联系人',
      contactinfo: '请输入始发地联系人联系方式',
      aimcontact: '请输入目的地联系人',
      aimcontactinfo: '请输入目的地联系人联系方式',
    }, _item = ''
    for (let i = 0, j = _objkey.length; i < j; i++) {
      if (!_objinfo[_objkey[i]]) {
        _status = true
        _item = objtext[_objkey[i]]
        break
      }
    }
    // _objkey.map(item => {
    //   if (!_objinfo[item]) {
    //     _status = true
    //   }
    // })
    if (_status) {
      return this.tipShowTaost(_item)
    } else if (!check) {
      return this.tipShowTaost('请阅读并同意《运输服务协议》')
    }

    let obj = {
      cdCarNo: `${_objinfo.framenum || ''}#${_objinfo.framenum1 || ''}`,
      cdCarType: `${item.cdCarModel}`,
      cdEndAddr: `${item.cdEnd}`,
      // cdEnd:`${item.cdEnd}`,
      cdEndConner: `${_objinfo.aimcontact}`,
      cdEndPhone: `${_objinfo.aimcontactinfo}`,
      cdRemark: `${_objinfo.remark || ''}`,
      // cdStart:`${item.cdStart}`,
      cdStartAddr: `${item.cdStart}`,
      cdStartConner: `${_objinfo.contact}`,
      cdStartDate: `${item.cdStartTime}`,
      cdStartPhone: `${_objinfo.contactinfo}`,
      ciNo: "string",
      payType: payType || '1',
      payIp: window.payIp,
      entId: item.entId,
      orStage: "2",
      orStatus: "22",
      orPrePrice: `${item.tqPrice}`,
      orPrice: `${item.tqPrice}`,
      orSale: `${item.creator}`,
      orSaleId: `${item.creatorId}`,
      orSaleComp: `${item.entName}`,
      orSaleCompId: item.entId,
      orSaleContact: `${item.creatorContact}`,
      tqId: qpId,
      cdId: item.id
    }
    // if (item.tqPrice > 3000) {
    //   return Taro.showToast({
    //     title: '支付金额超过3000元,请联系管理员线下支付'
    //   })
    // }
    this.setState({
      paramsPay: obj,
      ispayShow: true
    })

  }
  handlePay() {
    this.addOrderTrans()
  }
  tipShowTaost(title) {
    return Taro.showToast({
      title,
      icon: 'none'
    })
  }
  onHandleChange(check) {
    this.setState({
      check
    })
  }
  handleClick(value) {
    this.setState({
      active: value
    })
  }
  handleClose() {
    this.setState({
      ispayShow: false
    })
  }
  render() {
    const {
      item,
      _objinfo,
      check
    } = this.state
    const _objinfoConform = function (value) {
      this.setObjInfo(arguments[arguments.length - 1].bind, value)
    }
    return (
      <View className='b_shoporders'>
        <DNavBar title={'运输下单'} className='b_shoporders_nav'></DNavBar>
        <View className='b_shoporders__box'>
          <View className='b_transportorder__form'>
            <AtForm>
              <View className='b_transportorder__form__item'>
                <DAtInpute title='承运方企业' value={item.entName} icon='none'></DAtInpute>
                <DAtInpute title='承运方联系人' value={item.creator} icon='none'></DAtInpute>
                <DAtInpute title=' 联系方式' value={item.creatorContact} icon='none'></DAtInpute>
              </View>
              <View className='b_transportorder__form__item'>
                <Text className='b_transportorder__form_title'>始发地信息</Text>
                <DAtInpute require bind='contact' value={_objinfo.contact} onChange={_objinfoConform.bind(this)} title='始发地联系人' placeholder='请输入始发地联系人'></DAtInpute>
                <DAtInpute require type='phone' bind='contactinfo' title='联系方式' value={_objinfo.contactinfo} onChange={_objinfoConform.bind(this)} placeholder='请输入始发地联系人联系方式'></DAtInpute>
                <DAtInpute title='始发地地址' value={item.cdStart} icon='none'></DAtInpute>
              </View>
              <View className='b_transportorder__form__item'>
                <Text className='b_transportorder__form_title'>目的地信息</Text>
                <DAtInpute require bind='aimcontact' value={_objinfo.aimcontact} onChange={_objinfoConform.bind(this)} title='目的地联系人' placeholder='请输入目的地联系人'></DAtInpute>
                <DAtInpute require bind='aimcontactinfo' type='phone' value={_objinfo.aimcontactinfo} onChange={_objinfoConform.bind(this)} title='联系方式' placeholder='请输入目的地联系人联系方式'></DAtInpute>
                <DAtInpute title='目的地地址' value={item.cdEnd} icon='none'></DAtInpute>
              </View>
              <View className='b_transportorder__form__item'>
                <Text className='b_transportorder__form_title'>运输信息</Text>
                <DAtInpute title='运输费用' value={item.tqPrice} icon='none'></DAtInpute>
                <DAtInpute title='发车日期' value={item.cdStartTime} icon='none'></DAtInpute>
                <DAtInpute title='托运车辆1' value={(item.cdCarModel && item.cdCarModel.split('#').length > 1) ? item.cdCarModel.split('#')[0] : item.cdCarModel} icon='none'></DAtInpute>
                <DAtInpute bind='framenum' value={_objinfo.framenum} onChange={_objinfoConform.bind(this)} maxLength={17} title='车架号' placeholder='请输入车架号'></DAtInpute>
                {
                  (item.cdCarModel && item.cdCarModel.split('#').length > 1) && <DAtInpute title='托运车辆2' value={item.cdCarModel.split('#')[1]} icon='none'></DAtInpute>
                }
                {
                  (item.cdCarModel && item.cdCarModel.split('#').length > 1) && <DAtInpute bind='framenum1' value={_objinfo.framenum1} onChange={_objinfoConform.bind(this)} maxLength={17} title='车架号' placeholder='请输入车架号'></DAtInpute>
                }
                <DAtInpute bind='remark' value={_objinfo.remark} maxLength={500} onChange={_objinfoConform.bind(this)} title='备注' placeholder='可留言给承运方'></DAtInpute>
              </View>
            </AtForm>
          </View>
          <View className='b_transportorder__footer'>
            <DAgree value={check} onHandleChange={this.onHandleChange.bind(this)} text={'《运输服务协议》'} className='b_transportorder__footer_dagree'></DAgree>
            <View className='b_transportorder__footer_btn'>
              <Text className='b_transportorder__footer_font1'>需支付运输费：</Text>
              <Text className='b_transportorder__footer_font2'>￥{item.tqPrice}</Text>
              <AtButton className='item_btn' onClick={this.handlePay.bind(this)}>支付运输费</AtButton>
            </View>

          </View>
          <DModal isShow={this.state.isOpened} title="支付提示" text="是否已完成支付？" firstbtn="支付失败" lastbtn="支付完成" onFirst={this.payFuc.bind(this)} onCancel={this.payFuc.bind(this)} onLast={this.payFuc.bind(this, 1)}></DModal>
          <DPaymodel handleClose={this.handleClose.bind(this)} onCancel={this.onCancelPay.bind(this)} isShow={this.state.ispayShow} />
        </View>

      </View>
    )
  }
}
